export default class Helper {
  // 计算两点间距离
  static distance(la1, lo1, la2, lo2) {
    let La1 = (la1 * Math.PI) / 180.0;
    let La2 = (la2 * Math.PI) / 180.0;
    let La3 = La1 - La2;
    let Lb3 = (lo1 * Math.PI) / 180.0 - (lo2 * Math.PI) / 180.0;
    let s =
      2 *
      Math.asin(
        Math.sqrt(
          Math.pow(Math.sin(La3 / 2), 2) +
            Math.cos(La1) * Math.cos(La2) * Math.pow(Math.sin(Lb3 / 2), 2)
        )
      );
    s = s * 6378.137;
    s = Math.round(s * 10000) / 10000;
    s = s.toFixed(2);
    return s;
  }

  // 换算距离 m => km
  static handleOfDistance(distance){
    distance = distance > 1000 ?  (distance/1000).toFixed(1) + 'km' : distance + 'm';
    return distance;
  }

  // 将时间戳转换成日期格式
  static timestampToTime(timestamp) {
    let date = new Date(timestamp * 1000);//时间戳为10位需*1000，时间戳为13位的话不需乘1000
    let Y = date.getFullYear() + '-';
    let M = (date.getMonth()+1 < 10 ? '0'+(date.getMonth()+1) : date.getMonth()+1) + '-';
    let D = (date.getDate() < 10 ? '0' + date.getDate(): date.getDate()) + ' ';
    let h = (date.getHours()< 10 ? '0' + date.getHours(): date.getHours()) + ':';
    let m = (date.getMinutes() < 10 ? '0' + date.getMinutes(): date.getMinutes()) + ':';
    let s = (date.getSeconds() < 10 ? '0' + date.getSeconds(): date.getSeconds());
    return Y + M + D + h + m + s;
  }

}
